package com.wanxi.servlet;

import com.wanxi.model.ProductModel;
import com.wanxi.service.ProductService;
import com.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/findAll")
public class ProductFindAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //        1.乱码处理
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        2.得到前端的数据
        String name = req.getParameter("name");
        String enterprise = req.getParameter("enterprise");
        String sort = req.getParameter("sort");
        String introduce = req.getParameter("introduce");
        String style = req.getParameter("style");
//        3.封装页面数据
        ProductModel model = ProductModel.builder()
                .name(name)
                .sort(Tool.strToInt(sort))
                .style(Tool.strToInt(style))
                .introduce(introduce)
                .enterprise(enterprise)
                .build();
//        4.处理逻辑关系
        ProductService productService = new ProductService();
        List<ProductModel> result = productService.findAll(model);
//        5.将值返回给前端
        req.setAttribute("result", result);
        req.getRequestDispatcher("/product.jsp").forward(req, resp);
    }


}
